package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.production.dao.PumpDeviceDao;
import cn.com.surker.production.entity.PumpDevice;
import cn.com.surker.production.entity.vo.PumpDeviceVo;
import cn.com.surker.production.service.PumpDeviceService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 泵送设备表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-04
 */
public abstract class PumpDeviceBaseServiceImpl extends ServiceImpl<PumpDeviceDao, PumpDevice> implements PumpDeviceService {

	@Resource
	protected PumpDeviceDao pumpDeviceDao;

	public List<PumpDevice> queryCodeOrName(String code, String name, String carNo) {
		if (StringUtils.isAllParamNull(code, name, carNo)) {
			return null;
		}
		QueryWrapper<PumpDevice> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.and(e -> {
			if (StringUtils.isNotBlank(code)) {
				e.or().eq("code", code);
			}
			if (StringUtils.isNotBlank(name)) {
				e.or().eq("name", name);
			}
			if (StringUtils.isNotBlank(carNo)) {
				e.or().eq("car_no", carNo);
			}
		});
		List<PumpDevice> pumpDevices = pumpDeviceDao.selectList(qw);
		return pumpDevices;
	}

	@Override
	public List<PumpDevice> findOrgDevice(Long orgId) {
		List<PumpDevice> list = pumpDeviceDao.findByOrg(orgId);
		return list;
	}

	@Override
	public List<PumpDevice> findBySaleCon(Long orgId, Long saleConId) {
		List<PumpDevice> list = pumpDeviceDao.findBySaleCon(orgId, saleConId);
		return list;
	}

	@Override
	public List<PumpDevice> findByIndentId(Long orgId, Long indentId) {
		if (indentId == null) {
			return null;
		}
		List<PumpDevice> pumpDeviceList = pumpDeviceDao.findByIndentId(orgId, indentId);
		return pumpDeviceList;
	}

	@Override
	public IPage<PumpDeviceVo> selectPage(Long orgId, String name, Integer pageIndex, Integer limit) {
		QueryWrapper<PumpDevice> qw = new QueryWrapper<>();
		qw.eq("a.status", StateEnum.ENABLED.getCode());
		if (StringUtils.isNotBlank(name)) {
			qw.like("a.name", name);
		}
		PageHelper.startPage(pageIndex, limit);
		IPage<PumpDeviceVo> iPage = pumpDeviceDao.selectFullPage(new Page<>((pageIndex + limit) / limit, limit), qw);
		/**分页插件**/
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}

	/**
	 * 查询泵车信息
	 * @param contractId
	 * @return
	 */
	public List<PumpDevice> findByContractAll(Long contractId) {
		QueryWrapper<PumpDevice> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (!StringUtils.isNull(contractId))
			qw.notInSql("id", "select pump_device_id from production_pump_con_device " +
					" where status = 0 and pro_con_id = " + contractId);
		List<PumpDevice> deviceList = pumpDeviceDao.selectList(qw);
		return deviceList;
	}
}